package com.neuedu.converter;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.core.convert.converter.Converter;

public class String2DateConverter implements Converter<String, Date> {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	public Date convert(String str) {
		//String -> java.util.Date
		java.util.Date temp = null;
		try {
			temp = sdf.parse(str);
		} catch (ParseException e) {
			return null;
		}
		//java.util.Date -> java.sql.Date
		if(temp != null){
			java.sql.Date date = new java.sql.Date(temp.getTime());
			return date;
		}else{
			return null;
		}
	}

}
